package com.service.firstapp.controller;


import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.huawei.paas.cse.tcc.annotation.TccTransaction;
import org.apache.servicecomb.core.exception.CseException;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.CseSpringDemoCodegen", date = "2018-03-06T02:14:40.372Z")

@RestSchema(schemaId = "firstapp")
@RequestMapping(path = "/FirstApp",produces = {MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public class FirstappImpl {

    private static final Logger logger = LoggerFactory.getLogger(FirstappImpl.class);

    @Autowired
    private HelloWorld userFirstappDelegate;


    @XmlRootElement(name = "person")
    static class Person{
        private String name;
        private String id;

        public void setName(String name) {
            this.name = name;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getId() { return id; }

        public String getName() {
            return name;
        }
    }

    @RequestMapping(value = "/helloworld", method = RequestMethod.POST ,produces = {"application/xml;charset=UTF-8"})
    public String helloworld( @RequestBody Person person){

        System.out.println("helloworld");
        return userFirstappDelegate.helloworld(person.name);
    }

    @RequestMapping(value = "/helloworld_1", method = RequestMethod.POST ,produces = {"application/xml;charset=UTF-8"})
    public String helloworld1( @RequestBody Person person){

        return getShopCartInpuString();
    }

    @RequestMapping(value = "/helloworld_2", method = RequestMethod.GET,produces = {"application/xml;charset=UTF-8"} )
    public Object helloworld2(){
        Person person = new Person();
        person.setName("wjh");
        person.setId("007");
        if(1==1){
            throw new InvocationException(400,"异常","异常2");
        }
        return person;
    }

    public String getShopCartInpuString() {

        final String str = "<?xml version=\"1.0\" encoding=\"GBK\" ?>\n" +
                "<accept_in>\n" +
                "  <accept_id>100001542763</accept_id>\n" +
                "  <accept_info>\n" +
                "    <accept_city>591</accept_city>\n" +
                "    <accept_org_id>1010212</accept_org_id>\n" +
                "    <accept_org_name></accept_org_name>\n" +
                "    <accept_province>5910</accept_province>\n" +
                "  </accept_info>\n" +
                "  <log_info>\n" +
                "    <msg_id>4</msg_id>\n" +
                "    <tx_id>301390161502865814250003</tx_id>\n" +
                "  </log_info>\n" +
                "  <log_level></log_level>\n" +
                "  <operator_city>591</operator_city>\n" +
                "  <operator_id>1010699</operator_id>\n" +
                "  <operator_info>\n" +
                "    <operator_county>101</operator_county>\n" +
                "    <operator_home_area></operator_home_area>\n" +
                "    <operator_home_area_name></operator_home_area_name>\n" +
                "    <operator_level>5</operator_level>\n" +
                "    <operator_name>林莉莉</operator_name>\n" +
                "  </operator_info>\n" +
                "  <order_num>1</order_num>\n" +
                "  <process_code>OPC_TradeMergePay</process_code>\n" +
                "  <request_source>201028</request_source>\n" +
                "  <request_time>20170816144344</request_time>\n" +
                "  <sysfunc_id>10612002</sysfunc_id>\n" +
                "  <order_content>\n" +
                "    <accept_seq>1</accept_seq>\n" +
                "    <order_type>81011001</order_type>\n" +
                "    <order_req>\n" +
                "      <object_info>\n" +
                "        <obj_city>591</obj_city>\n" +
                "        <obj_type>1</obj_type>\n" +
                "        <obj_id>40</obj_id>\n" +
                "        <obj_number>1</obj_number>\n" +
                "      </object_info>\n" +
                "     <cart_id>100000003806</cart_id>\n" +
                "    </order_req>\n" +
                "  </order_content>\n" +
                "</accept_in>\n";

        return str;
    }

    @TccTransaction(confirmMethod = "confirm", cancelMethod = "cancel")
    @RequestMapping(value = "/dotry", method = RequestMethod.POST)
    public void doTry() {
        logger.info("==========doTry==========");
    }

    public void confirm() {
        logger.info("==========confirm==========");
    }

    public void cancel() {
        logger.info("==========cancel==========");
    }

    @ExceptionHandler(value=InvocationException.class)
    public String allExceptionHandler(Exception exception) throws Exception
    {
        exception.printStackTrace();
        System.out.println("我报错了："+exception.getLocalizedMessage());
        System.out.println("我报错了："+exception.getCause());
        System.out.println("我报错了："+exception.getSuppressed());
        System.out.println("我报错了："+exception.getMessage());
        System.out.println("我报错了："+exception.getStackTrace());
        return "FirstApp服务器异常，请联系管理员！";
    }

//    @RequestMapping(value = "/test1", method = RequestMethod.POST)
//    public void test() {
//        logger.info("==========test==========");
//    }
}
